"""互动相关路由"""
from fastapi import APIRouter, Depends, HTTPException, Query
from sqlalchemy.orm import Session
from app.config.database import get_db
from app.controllers.interaction_controller import interaction_controller
from app.middlewares.auth import get_current_user
from app.schemas.interaction import CommentRequest
from app.schemas.common import ResponseModel, PageResponse

router = APIRouter(prefix="/api/interactions", tags=["interactions"])


@router.post("/like/{video_id}", response_model=ResponseModel[dict])
def toggle_like(
    video_id: int,
    db: Session = Depends(get_db),
    current_user_id: int = Depends(get_current_user)
):
    """切换视频点赞状态"""
    try:
        result = interaction_controller.toggle_like(db, current_user_id, video_id)
        return ResponseModel(
            code=200,
            message="操作成功",
            data=result
        )
    except ValueError as e:
        raise HTTPException(status_code=400, detail=str(e))
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))


@router.post("/favorite/{video_id}", response_model=ResponseModel[dict])
def toggle_favorite(
    video_id: int,
    db: Session = Depends(get_db),
    current_user_id: int = Depends(get_current_user)
):
    """切换视频收藏状态"""
    try:
        result = interaction_controller.toggle_favorite(db, current_user_id, video_id)
        return ResponseModel(
            code=200,
            message="操作成功",
            data=result
        )
    except ValueError as e:
        raise HTTPException(status_code=400, detail=str(e))
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))


@router.post("/comment/{video_id}", response_model=ResponseModel[dict])
def add_comment(
    video_id: int,
    comment_data: CommentRequest,
    db: Session = Depends(get_db),
    current_user_id: int = Depends(get_current_user)
):
    """添加视频评论"""
    try:
        comment = interaction_controller.add_comment(db, current_user_id, video_id, comment_data)
        return ResponseModel(
            code=200,
            message="评论成功",
            data={
                "id": comment.id,
                "content": comment.content,
                "parent_id": comment.parent_id,
                "created_at": comment.created_at.isoformat() if comment.created_at else None
            }
        )
    except ValueError as e:
        raise HTTPException(status_code=400, detail=str(e))
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))


@router.get("/comments/{video_id}", response_model=ResponseModel[PageResponse])
def get_video_comments(
    video_id: int,
    page: int = Query(1, ge=1, description="页码"),
    pageSize: int = Query(20, ge=1, le=50, description="每页数量"),
    db: Session = Depends(get_db)
):
    """获取视频评论列表"""
    try:
        result = interaction_controller.get_video_comments(db, video_id, page, pageSize)
        return ResponseModel(
            code=200,
            message="success",
            data=result
        )
    except ValueError as e:
        raise HTTPException(status_code=400, detail=str(e))
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))


@router.get("/replies/{comment_id}", response_model=ResponseModel[PageResponse])
def get_comment_replies(
    comment_id: int,
    page: int = Query(1, ge=1, description="页码"),
    pageSize: int = Query(20, ge=1, le=50, description="每页数量"),
    db: Session = Depends(get_db)
):
    """获取评论的回复列表"""
    try:
        result = interaction_controller.get_comment_replies(db, comment_id, page, pageSize)
        return ResponseModel(
            code=200,
            message="success",
            data=result
        )
    except ValueError as e:
        raise HTTPException(status_code=400, detail=str(e))
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))


@router.delete("/comment/{comment_id}", response_model=ResponseModel[dict])
def delete_comment(
    comment_id: int,
    db: Session = Depends(get_db),
    current_user_id: int = Depends(get_current_user)
):
    """删除评论（仅评论作者可删除）"""
    try:
        success = interaction_controller.delete_comment(db, comment_id, current_user_id)
        return ResponseModel(
            code=200,
            message="删除成功",
            data={"success": success}
        )
    except ValueError as e:
        raise HTTPException(status_code=400, detail=str(e))
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))
